Standard coding techniques


Coding style and methods are very important issues for real-world programmers. To
make your code clear and descriptive to help other programmers understand your programs
and even to make your old programs easy in maintenance and debugging; you have to
follow standard coding techniques.

There are many coding standards. They are slightly different from each other, but
they share the most important structures. If you are Delphi programmer, or any Borland
development tool programmer, then you have to follow Borland developers standard
coding. Borland standard coding can be observed in any Delphi run-time libraries
such as System.pas, SysUtils.pas, Dialogs.pas, and other unit sources. We will mention
here some of important Borland coding standards:


Variables, Procedures, and Functions name:

Variable, procedure, and function name must be descriptive, for example instead
of using
N for user name it is better to make it long variable name such as: UserName
, but do not use very long names such as
UserNameHowUseMyApplication.
The second important issue of naming is capitalization. You have to capitalize the
first letter of each word such as:

Counter
AddNewRecord
MessageDlg
DisplayHelpFile
RegisteredUserName

You can see the difference if we do not care about capitalization:

counter
addnewrecord
messagedlg
displayhelpfile
registeredusername

It looks more ugly and ambiguous than the previous examples.



Reserved words

Reserved words must be written in lower case such as:

var
begin
string



Standard types, functions, and procedures

First letter of standard types, functions, and procedures must be capitalized such
as:

Integer
Boolean
Writeln

Above standard types and procedures are predefined in standard Delphi units such
as System and SysUtils units.

User defined data types

User defined data types must begin with T letter such as:

TMyType
TRec

Pointers must begin with P letter such as:

PNode = ^ TNode;
TNode = Record

Enumerated types must begin with two small letters which describe enumerated type
such as:

Type
T
FieldType = (ftInteger, ftString, ftByte);
T
Color = (clRed, clGreen, clBlue, clWhite);


Margins

Var, type, const, uses, interface, implementation, procedure, function, and other
alike keywords must be written closed to the begining of line without any space such
as:

var
 i: integer;

implementation

Uses
Windows;

procedure
....
begin
end;

Procedure body and variable declarations must be written after two spaces from the
left such as:

var
x, y: integer;
 Average: Double;

procedure DoSomeThing;
begin
 Beep;
ShowMessage('Hello there');
end;

Begin and end must be in separate lines and in the same level such as:

 for i:= 1 to 100 do
begin
  DoSomeThing;
  DoAnotherThing;
end;

Loops and if statements must increase it's statements two spaces to right such as:

procedure A;
var
i: integer;
begin
for i:= 0 to 10 do
begin
 
DoSomeThing;
  if i = 2 then
   
Beep;    
  if i = 3 then
  begin
   
DoAnotherThing;
    Beep;
   end; // if i = 3
end; // for i
end;


Function and procedure calling

You should call functions and procedures according to this format:

 CallProc(Par1, Par2, Par3..)

Examples:

 ShowMessage('Hello');
AddRec(Name, Age, Address);

If parameters are very long you can call it as:

MessageDlg('Borland coding standards',
mtInformation, [mbOk],
 0);


Operators

See below examples to see where you must put space with operators:

 X:= Y * 2;
Z:= ( X
/ Y) * 2;
if UserName = Name then
  Break;
if ( X > Y ) or ( Z <> y ) then
  Exit;
 

Do not do this

- Do not use global variables. Global variables make the program hard to be understood
and update, but you may need to use it in some cases.

- Do not use goto. Goto breaks program structure.

- Do not use Real type. Real type exists only for backward compatibility and it
reduces CPU performance. Instead use Double, Single, and Extended floating point
types.

- Do not use Variant data type until you really need it.


See also

Naming conventions